﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using Aeon.Emulator.DebugSupport;

namespace Aeon.Emulator.Decoding.Recompiler
{
    /// <summary>
    /// A block of recompiled instructions.
    /// </summary>
    internal sealed class CompiledBlock
    {
        private readonly Func<VirtualMachine, int> compiledMethod;

        /// <summary>
        /// Initializes a new instance of the CompiledBlock class.
        /// </summary>
        /// <param name="method">Recompiled method.</param>
        public CompiledBlock(Func<VirtualMachine, int> method)
        {
            this.compiledMethod = method;
        }

        /// <summary>
        /// Gets the instructions emulated by this block.
        /// </summary>
        public Instruction[] Instructions { get; private set; }

        /// <summary>
        /// Invokes the block.
        /// </summary>
        /// <param name="vm">VirtualMachine instance against which the block is run.</param>
        /// <returns>Number of instructions run.</returns>
        public int Run(VirtualMachine vm)
        {
            return this.compiledMethod(vm);
        }
        /// <summary>
        /// Sets the instructions emulated by this block.
        /// </summary>
        /// <param name="instructions">Instructions emulated by the block.</param>
        [Conditional("DEBUG")]
        public void SetInstructions(IEnumerable<Instruction> instructions)
        {
            this.Instructions = instructions.ToArray();
        }
    }
}
